package chenqianghao.rbac.service;

import chenqianghao.rbac.domain.dto.UserDto;
import chenqianghao.rbac.domain.entity.Dept;
import chenqianghao.rbac.domain.entity.Role;
import chenqianghao.rbac.domain.entity.User;
import chenqianghao.rbac.domain.vo.Result;
import chenqianghao.rbac.domain.vo.UserVo;

import java.util.Arrays;

/**
 * 业务层-用户
 *
 * @author 陈蔷好
 * @since 2025/4/7 17:10
 */
public class UserService {

    public static int DATA_SIZE = 100;

    public static User[] data = new User[DATA_SIZE + 1];

    public static int idCounter = 1;

    public static UserVo convertToVo(User user) {
        String deptName = "";
        Dept dept = DeptService.findById(user.deptId);
        if(dept != null) {
            deptName = dept.name;
        }

        String roleName = "";
        Role role = RoleService.findById(user.roleId);
        if(role != null) {
            roleName = role.name;
        }
        return new UserVo(user.id, user.username, user.name, user.mobile, user.email, user.deptId,deptName,
                user.roleId,roleName,user.enabled);
    }

    public static User createEntity(UserDto dto, int id) {
        User user = new User(dto.username, dto.name, dto.mobile, dto.email, dto.deptId, dto.roleId); // Ctrl+p
        user.id = id;
        return user;
    }

    public static void updateEntity(UserDto dto, User user) {
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }

    public static boolean deptInUse(int deptId) {
        boolean inUse = false;
        for(User user: data) {
            if(user!=null && user.deptId==deptId) {
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    public static boolean roleInUse(int roleId) {
        boolean inUse = false;
        for(User user: data) {
            if(user != null && user.roleId ==roleId) {
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    public static boolean existsById(int id) {
        return id < 1 && id >= idCounter && data[id] == null;
    }

    public static Result list() {
        UserVo[] temp = new UserVo[DATA_SIZE];
        int length = 0;
        for (int i = 1; i < idCounter; i++) {
            if (data[i] != null) {
                temp[length++] = convertToVo(data[i]);
            }
        }
        return Result.success("查询成功", Arrays.copyOf(temp, length));
    }

    public static Result create(UserDto dto) {
        if (idCounter > DATA_SIZE) {
            return Result.failure("创建失败，超出了储存容量");
        }
        if (!DeptService.existsById(dto.deptId)) {
            return Result.failure("创建失败，不存在指定的部门");
        }
        if (!RoleService.existsById(dto.roleId)) {
            return Result.failure("创建失败，不存在指定的角色");
        }
        User user = createEntity(dto, idCounter++);
        data[user.id] = user;
        return Result.success("创建成功", null);
    }

    public static Result update (UserDto dto , int id) {
        if(existsById(id)) {
            return Result.failure("更新失败，不存在该数据");
        }
        if (!DeptService.existsById(dto.deptId)) {
            return Result.failure("创建失败，不存在指定的部门");
        }
        if (!RoleService.existsById(dto.roleId)) {
            return Result.failure("创建失败，不存在指定的角色");
        }
        User user = data[id];
        updateEntity(dto , user);
        return Result.success("更新成功" , null);
    }

    public static Result toggle(int id) {
        if(!existsById(id)) {
            return Result.failure("切换状态失败，不存在该数据");
        }
        data[id].enabled = !data[id].enabled;
        return Result.success(data[id].enabled ? "启用成功" : "停用成功", null);
    }
}
